package com.LeeCode;

/**
 * 子序列首尾元素的最大乘积
 */

public class Code3584 {
    public static void main(String[] args) {
        int[] nums = {1, 3, -5, 5, 6, -4};
        int m = 3;
        System.out.println(new Code3584().maximumProduct(nums, m));
    }

    public long maximumProduct(int[] nums, int m) {
        long ans = Long.MIN_VALUE, maxVal = nums[0], minVal = nums[0];
        for (int i = m - 1; i < nums.length; i++) {
            int index = i - m + 1;
            if (maxVal < nums[index]) maxVal = nums[index];
            if (minVal > nums[index]) minVal = nums[index];

            ans = Math.max(ans, Math.max(nums[i] * maxVal, nums[i] * minVal));
        }
        return ans;
    }
}
